package com.milo.concurrent.locks;


import org.junit.Test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: Milogenius
 * @create: 2019-04-23 08:40
 * @description:
 **/
public class LockTest {

    private int count = 0;

    /**
     * 定义锁
     */
    private Lock lock = new ReentrantLock();


    /**
     * 同步代码块
     *
     * @return
     */
    @Test
    public void test1() {
        synchronized (this) {
            ++count;
            System.out.println(count);
        }

    }

    @Test
    public void test2() {
        lock.lock();
        int newCount = ++count;
        lock.unlock();
        System.out.println(newCount);

    }

}
